$(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		if($.validate.form()) {
			register();
		}
	}
});

function register() {
	console.log('rrrrr')
	$.modal.loading($("#btnSubmit").data("loading"));
	var loginname = $.common.trim($("input[name='loginname']").val());
	var username = $.common.trim($("input[name='username']").val());
	var password = $.common.trim($("input[name='password']").val());
	var phone = $.common.trim($("input[name='phone']").val());
	$.ajax({
		type : "post",
		url : ctx + "admin/user/register",
		data : {
			"loginname" : loginname,
			"username" : username,
			"password" : password,
			"phone" : phone
		},
		success : function(r) {
			if (r.code == 0) {
				layer.alert("<font color='red'>恭喜你，您的账号 " + loginname
						+ " 注册成功！</font>", {
					icon : 1,
					title : "系统提示"
				}, function(index) {
					// 关闭弹窗
					layer.close(index);
					location.href = ctx + 'index';
				});
			} else {
				$.modal.closeLoading();
				$.modal.msg(r.msg);
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#registerForm").validate({
		rules : {
			loginname : {
				required : true,
				minlength : 2,
				remote : {
					type : "post",
					url : ctx + "admin/user/checkAccount",
					data : {
						isowner: 'false',
						accountName : function() {
							return $("#loginname").val();
						}
					}
				}
			},
			username : {
				required : true,
				minlength : 1
			},
			phone : {
				required : true,
				isPhone : true,
				remote : {
					type : "post",
					url : ctx + "admin/user/checkPhoneUnique",
					data : {
						userid: '',
						isowner: 'false',
						phone : function() {
							return $("#phone").val();
						}
					}
				}
			},
			password : {
				required : true,
				minlength : 6
			},
			confirmPassword : {
				required : true,
				equalTo : "[name='password']"
			}
		},
		messages : {
			loginname : {
				required : icon + "请输入您的登录名",
				minlength : icon + "登录名不能小于2个字符",
				remote : "登录名已经存在"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名不能小于1个字符"
			},
			phone : {
				required : "请输入手机号码",
				isPhone : "请输入正确的手机号码",
				remote : "手机号码已经存在"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码不能小于6个字符",
			},
			confirmPassword : {
				required : icon + "请再次输入您的密码",
				equalTo : icon + "两次密码输入不一致"
			}
		}
	})
}
